//
//  YYEOriginalStatusView.swift
//  weibo14
//
//  Created by mac on 16/3/7.
//  Copyright © 2016年 mac. All rights reserved.
//
// 公共的间距   原创微博

import UIKit
import SnapKit
import YYText



let YYEStatusCellMargin: CGFloat = 8
// 头像宽高
let YYEStatusCellHeadImageWH: CGFloat = 35

class YYEOriginalStatusView: UIView {
    
    //拿到约束
    var bottomConstraint: Constraint?

    var statusViewModel: YYEStatusViewModel? {
    
        didSet{
        
            if let urlString = statusViewModel?.status?.user?.profile_image_url,let url = NSURL(string: urlString){
            
                headImage.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default"))
            }
        
            // 设置昵称
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            
            
            // 设置时间和来源 TODO: 以后会专门处理
            // timeLabel.text = statusViewModel?.status?.created_at
            timeLabel.text = statusViewModel?.createAtText
            
            // timeLabel.text = statusViewModel?.status?.created_date?.dateDescription
            
            
            // sourceLabel.text = statusViewModel?.status?.source
            // 直接用处理好的字符串  经过处理的截取的
            sourceLabel.text = statusViewModel?.sourceText
            
            
            // 设置认证图标
            verifiedImage.image = statusViewModel?.verifiedImage
            // 设置vip图标
            membershipImage.image = statusViewModel?.membershipImage
            // 设置内容
            //contentLabel.text = statusViewModel?.status?.text
            //contentLabel.attributedText =
            
            //contentLabel.attributedText = statattributedString.addAttributeusViewModel?.originalAttributedString
            contentLabel.attributedText = statusViewModel?.originalAttributedString

            // 设置内容大小
            contentLabel.snp_updateConstraints { (make) -> Void in
                print(statusViewModel!.originalAttrSize!)
                make.size.equalTo(statusViewModel!.originalAttrSize!)
            }

            // 设置内容大小
            contentLabel.snp_updateConstraints { (make) -> Void in
              //  print(statusViewModel!.originalAttrSize!)
                
            
                
                make.size.equalTo(statusViewModel!.originalAttrSize!)
            }

            
            // 卸载原来的
            bottomConstraint?.uninstall()
            
            // 重新附的值 找着解决是否隐藏veiw
            if let pics = statusViewModel?.status?.pic_urls where pics.count > 0 {
                // 有数据
                pictureView.hidden = false
                // 把你自己需要的数据传过去
                pictureView.picUrls = pics

                
                // 更改约束
                self.snp_updateConstraints{ (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(pictureView).offset(YYEStatusCellMargin).constraint
                }
                
            } else {
                // 没有数据
                pictureView.hidden = true
            
                // 更改约束
                self.snp_updateConstraints{ (make) -> Void in
                bottomConstraint = make.bottom.equalTo(contentLabel).offset(YYEStatusCellMargin).constraint
                }
                
            }
            
            
        }
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
        // 添加子控件
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImage)
        addSubview(membershipImage)
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        // 添加约束
        
        headImage.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(YYEStatusCellMargin)
            make.size.equalTo(CGSize(width: YYEStatusCellHeadImageWH, height: YYEStatusCellHeadImageWH))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage.snp_trailing).offset(YYEStatusCellMargin)
            make.top.equalTo(headImage)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImage)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(YYEStatusCellMargin)
            make.top.equalTo(timeLabel)
        }
        
        verifiedImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_trailing).offset(-3)
            make.centerY.equalTo(headImage.snp_bottom).offset(-3)
        }
        
        membershipImage.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(YYEStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImage)
            make.top.equalTo(headImage.snp_bottom).offset(YYEStatusCellMargin)
           // make.trailing.equalTo(self).offset(-YYEStatusCellMargin)
        }
        
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
         //   make.size.equalTo(CGSizeMake(100, 100))
                
                
            make.top.equalTo(contentLabel.snp_bottom).offset(YYEStatusCellMargin)
        }
        
        
        
        
        // 添加当前视图的底部等于 contentLabel 的底部加上一个间距
        self.snp_makeConstraints { (make) -> Void in
          bottomConstraint = make.bottom.equalTo(pictureView).offset(YYEStatusCellMargin).constraint
        }
        
        contentLabel.highlightTapAction = { (_, attr, range: NSRange, _) in
            print("高亮文字点击了")
            // 取到对应位置的属性
            let attribute = attr.yy_attribute(YYTextHighlightAttributeName, atIndex: UInt(range.location)) as? YYTextHighlight
            // 怎么才能取到点击的 YYTextHighLighted
            print(attribute?.userInfo["text"])
            
        }

    }
    // MARK: - 懒加载控件
    
    // 头像
    private lazy var headImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
    
    // 昵称
    private lazy var nameLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSzie: 15)
        label.text = "隔壁老王"
        return label
    }()
    
    // 微博创建时间的label
    private lazy var timeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), fontSzie: 12)
        label.text = "1小时前"
        return label
    }()
    
    // 微博来源的label
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSzie: 12)
        label.text = "来自 烧烧微博"
        return label
    }()
    
    // 认证
    private lazy var verifiedImage: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "avatar_vip")
        return imageView
    }()
    
    // 会员图标
    private lazy var membershipImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    // 显示微博文字的label
    private lazy var contentLabel: YYLabel = {
        let label = YYLabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()

        label.numberOfLines = 0
        return label
    }()
    
    // 图片框
    private lazy var pictureView:YYEStatusPictureView = {
    
        let view = YYEStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        return view
        
    }()
    
    
}
